package cn.dt70.controller;

import cn.dt70.pojo.AqbMsgconfig;
import cn.dt70.pojo.AqbUser;
import cn.dt70.pojo.AqbUserMsg;
import cn.dt70.service.AqbMsgconfigService;
import cn.dt70.service.AqbUserMsgService;
import cn.dt70.utils.Constants;
import cn.dt70.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/msg/")
public class AqbMsgController {

    @Resource
    private AqbUserMsgService aqbUserMsgService;

    @Resource
    private AqbMsgconfigService aqbMsgconfigService;

    /*查询用户站内信*/
    @RequestMapping("getAll")
    public String getAll(int status, Integer currentPageNo, HttpSession session, PageSupport pageSupport, Model model){
        AqbUser aqbUser=(AqbUser) session.getAttribute(Constants.USER_SESSION);
        Integer uid=aqbUser.getUserId();
        pageSupport.setTotalCount(aqbUserMsgService.getTotalCount(status,uid));
        System.out.println("当前页码currentPageNo:"+currentPageNo);
        pageSupport.setCurrentPageNo(currentPageNo);
        List<AqbUserMsg> msgs=aqbUserMsgService.getAllMsg(uid,status,(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize(),pageSupport.getPageSize());
        model.addAttribute("msgs",msgs);
        model.addAttribute("currentPageNo",pageSupport.getCurrentPageNo());
        model.addAttribute("totalCount",pageSupport.getTotalCount());
        model.addAttribute("totalPageCount",pageSupport.getTotalPageCount());
        model.addAttribute("status",status);
        return "usercenter-messagecontrol-sitemsg";
    }

    @RequestMapping("upd")
    @ResponseBody
    public String updCfg(AqbMsgconfig aqbMsgconfig){
        aqbMsgconfigService.updConfig(aqbMsgconfig);
        return "";
    }

    @RequestMapping("del")
    @ResponseBody
    public String del(int uid){
        int result= aqbMsgconfigService.delConfigByUid(uid);
        if(result==1){
            return "{\"result\":\"T\"}";
        }
        return "{\"result\":\"F\"}";
    }

    @RequestMapping("show")
    public String show(){
        return "usercenter-messagecontrol-set_message";
    }

    @RequestMapping("cfgupd")
    @ResponseBody
    public String cfgupd(AqbMsgconfig aqbMsgconfig,HttpSession session){
        session.setAttribute("msgCfg",aqbMsgconfig);
        aqbMsgconfigService.updConfig(aqbMsgconfig);
        return "{\"msgCfg\":aqbMsgconfig}";

    }

}